package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.context.Config;
import cn.livingCloud.context.SessionConstants;
import cn.livingCloud.dto.CaseCategoryDto;
import cn.livingCloud.dto.front.CaseAccusationFrontFromDto;
import cn.livingCloud.entity.core.CaseCategory;
import cn.livingCloud.entity.core.Employee;
import cn.livingCloud.entity.system.Setting;
import cn.livingCloud.entity.user.Member;
import cn.livingCloud.repository.CaseCategoryMapper;
import cn.livingCloud.repository.EmployeeMapper;
import cn.livingCloud.repository.SettingMapper;
import cn.livingCloud.utils.SessionUtils;
import cn.livingCloud.wechat.protocol.JsSDKSignature;

import java.util.List;

/**
 * Created by 唐旭 on 2016/6/13.
 */
public class CaseAccusationFrontFromDtoLoader {
    private transient EmployeeMapper employeeMapper = BeanProvider.getBean(EmployeeMapper.class);
    private transient SettingMapper settingMapper = BeanProvider.getBean(SettingMapper.class);
    private transient CaseCategoryMapper categoryMapper = BeanProvider.getBean(CaseCategoryMapper.class);

    private String request;

    public CaseAccusationFrontFromDtoLoader(String request) {
        this.request = request;
    }

    public CaseAccusationFrontFromDto load() {
        //获取jssdk
        Setting setting = settingMapper.findSetting();
        JsSDKSignature jsSDKSignature = new JsSDKSignature(request, setting.getAppid(), setting.getSecret());

        //获取当前粉丝(创建人)
        Member currentMember = (Member) SessionUtils.getCurrentMember();
        CaseAccusationFrontFromDto caseAccusationFrontFromDto = new CaseAccusationFrontFromDto(jsSDKSignature);
        caseAccusationFrontFromDto.setCreatorUuid(currentMember.getUuid());
        testLocation(caseAccusationFrontFromDto);

        loadCategoryList(caseAccusationFrontFromDto);

        return caseAccusationFrontFromDto;
    }

    private void loadCategoryList(CaseAccusationFrontFromDto fromDto) {
        Member currentMember = (Member) SessionUtils.getCurrentMember();
        Employee employee = employeeMapper.findEmployeeByMember(currentMember.getUuid());
        if (employee != null) {
            SessionUtils.setAttribute(SessionConstants.EMPLOYEE_KEY, employee);
            List<CaseCategoryDto> categoryList = fromDto.getCategoryList();
            List<CaseCategory> all = categoryMapper.getAll();
            for (CaseCategory category : all) {
                if (category.getParent() == null) {
                    categoryList.add(new CaseCategoryDto(category));
                }
            }
            addSubCategory(categoryList, all);
        }
    }

    private void addSubCategory(List<CaseCategoryDto> categoryList, List<CaseCategory> all) {
        for (CaseCategoryDto caseCategoryDto : categoryList) {
            List<CaseCategoryDto> subList = caseCategoryDto.getSubList();
            for (CaseCategory category : all) {
                if (caseCategoryDto.getUuid().equals(category.getParentUuid())) {
                    subList.add(new CaseCategoryDto(category));
                }
            }
            addSubCategory(subList, all);
        }
    }

    @Deprecated
    private void testLocation(CaseAccusationFrontFromDto caseAccusationFrontFromDto) {
        if (Config.ENVIRONMENT.equals("development")) {
            caseAccusationFrontFromDto.setLocationX(104.072258f);
            caseAccusationFrontFromDto.setLocationY(30.663422f);
        }
    }
}
